//
//  Array+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit
import ObjectMapper

public extension Array where Element: Hashable {
    
    // 无序去重
    func removingDuplicates() -> [Element] {
        var addedDict = [Element: Bool]()
        return filter {
            addedDict.updateValue(true, forKey: $0) == nil
        }
    }
    
    mutating func removeDuplicates() {
        self = self.removingDuplicates()
    }
    
}

public extension Array {
    
    // 自定义模型数组去重
    func filterDuplicates<E: Equatable>(_ filter:(Element) -> E) -> [Element] {
        var result = [Element]()
        for value in self {
            let key = filter(value)
            if !result.map({ filter($0) }).contains(key){
                result.append(value)
            }
        }
        return result
    }
}
